package com.zmn.oms.zmn.business.interfaces.open.v2;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.common.constant.OpenApiConsts;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.utils.SignUtil;
import com.zmn.oms.model.dto.open.v2.PushOrderDTO;
import com.zmn.oms.model.entity.notifylog.OrderNotifyLog;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 类描述：
 *
 * @author LiangHailong
 * @date 2019/12/23 18:33
 */
public interface V2OrderStatusSyncService {

    /**
     * 推送新单
     * @param orderWorkLogChange
     */
    ResultDTO pushNewOrder(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送预约时间
     * @param orderWorkLogChange
     */
    ResultDTO pushDutyTime(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送接单工程师信息
     * @param orderWorkLogChange
     */
    ResultDTO pushMasterInfo(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送工程师上门
     * @param orderWorkLogChange
     */
    ResultDTO pushMasterVisit(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送订单状态为挂起
     * @param orderWorkLogChange
     */
    ResultDTO pushOrderSuspend(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送订单状态为完成
     * @param orderWorkLogChange
     */
    ResultDTO pushServiceComplete(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送订单状态为完成
     * @param orderWorkLogChange
     */
    ResultDTO pushOrderComplete(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送订单状态为取消
     * @param orderWorkLogChange
     */
    ResultDTO pushOrderCancel(OrderWorkLogChange orderWorkLogChange);

    /**
     * 推送商家备注信息（只针对厂商订单）
     * @param orderWorkLogChange
     */
    ResultDTO pushFactoryRemark(OrderWorkLogChange orderWorkLogChange);

    /**
     * 封装请求参数
     * @param channelId
     * @param secretkey
     * @param funId
     * @param pushOrderDTO
     * @return
     */
    default Map<String, String> newRequestParams(int channelId, String secretkey, String funId,
                                                 PushOrderDTO pushOrderDTO, String version) {
        Map<String, String> params = Maps.newHashMap();
        params.put("appId", String.valueOf(channelId));
        params.put("jsonData", JSON.toJSONString(pushOrderDTO));
        params.put("funId", funId);
        params.put("version", version);
        params.put("timestamp", String.valueOf((System.currentTimeMillis() / 1000)));
        params.put("sign", SignUtil.signMd5(params, secretkey));

        return params;
    }

    /**
     * 返回结果转化
     *
     * @param orderWorkLogChange
     * @param notifyStatus
     * @param response
     * @return
     */
    default ResultDTO toResultDTO(OrderWorkLogChange orderWorkLogChange, int notifyStatus, String response) {
        return toResultDTO(orderWorkLogChange, notifyStatus, response, OrderConsts.ORDER_NOTIFY_TYPE_CHANGE);
    }

    /**
     * 返回结果转化
     *
     * @param orderWorkLogChange
     * @param notifyStatus
     * @param response
     * @return
     */
    default ResultDTO toResultDTO(OrderWorkLogChange orderWorkLogChange, int notifyStatus, String response, int notifyType) {
        OrderNotifyLog orderNotifyLog = new OrderNotifyLog();
        orderNotifyLog.setMqMsgKey(orderWorkLogChange.getMqMsgKey());
        orderNotifyLog.setOrderId(orderWorkLogChange.getOrderId());
        orderNotifyLog.setChannelId(orderWorkLogChange.getChannelId());
        orderNotifyLog.setOrderStatus(orderWorkLogChange.getStatus());
        orderNotifyLog.setOrderResultStatus(orderWorkLogChange.getResultStatus());
        orderNotifyLog.setNotifyStatus(String.valueOf(notifyStatus));
        orderNotifyLog.setType(notifyType);
        orderNotifyLog.setNotifyTime(DateUtil.getNow());
        String notifyStatusName = OpenApiConsts.getNotifyStatusName(notifyStatus);

        orderNotifyLog.setNotifyResultMsg(String.format("[%s]-%s", notifyStatusName, response));

        ResultDTO resultDTO = new ResultDTO();
        if (StringUtils.isNotBlank(response)) {
            try {
                ResultDTO responseDTO = JSON.parseObject(response, ResultDTO.class);
                resultDTO.setStatus(responseDTO.getStatus());
                resultDTO.setStatusBool(responseDTO.isStatusBool());
                resultDTO.setMessage(responseDTO.getMessage());
            } catch (Exception ex) {
            }
        }

        resultDTO.setData(orderNotifyLog);
        return resultDTO;
    }
}
